package com.wandian.ui.controls
{
	import utils.Res;

	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	import com.wandian.utils.GDI;

	public class WanButton extends Sprite
	{
		private var key:String;
		private var clickHandler:Function
		private var enable:Boolean=true;

		public function WanButton(key:String)
		{
			super();
			setKey(key);
			this.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onUp);
			this.addEventListener(MouseEvent.CLICK, onClick);

		}

		public function setKey(key:String):void
		{
			this.key=key
			onUp();
		}

		public function setClickHandler(handler:Function):void
		{
			this.clickHandler=handler
		}

		private function onOver(e:MouseEvent):void
		{
			if (!enable)
			{
				return
			}
			var bmd:BitmapData=Res.get(key + "_btn_over")
			if (bmd == null)
			{
				return
			}
			var g:Graphics=this.graphics
			g.clear();

			GDI.drawBitmapData(g, 0, 0, bmd);
		}

		private function onUp(e:MouseEvent=null):void
		{
			if (!enable)
			{
				return
			}
			var g:Graphics=this.graphics
			g.clear();
			GDI.drawBitmapData(g, 0, 0, Res.get(key + "_btn"));

		}

		private function onDisable():void
		{

			var bmd:BitmapData=Res.get(key + "_btn_disable")
			if (bmd == null)
			{
				return
			}
			var g:Graphics=this.graphics
			g.clear();
			GDI.drawBitmapData(g, 0, 0, bmd);

		}

		private function onClick(e:MouseEvent):void
		{
			if (!enable)
			{
				return
			}
			if (clickHandler != null)
			{
				this.clickHandler();
			}
		}

		public function setEnable(f:Boolean):void
		{
			enable=f
			if (enable)
			{
				onUp();
			}
			else
			{
				onDisable();
			}
		}

	}
}
